package com.baoyanghui.servlet.customer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.baoyanghui.common.Constants;
import com.baoyanghui.common.InterfaceAccess;
import com.baoyanghui.config.Configs;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("请使用POST方式调用");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		JSONObject result = null;
		int code = 9999;
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			request.setCharacterEncoding("UTF-8");
			params.put("customer_id", request.getParameter("customer_id"));
			params.put("password", request.getParameter("password"));
			params.put("email", request.getParameter("email"));
			
			result = InterfaceAccess.callInterface("/customer/register", params);
			code = result.getInt("code");
			
			if (code == 0) {
				request.getSession().setAttribute(Configs.SESSION_USER_ID, result.getJSONObject("resultData").getString("userid"));
				request.getSession().setAttribute(Configs.SESSION_USERCODE, result.getJSONObject("resultData").getString("usercode"));
				request.getSession().setAttribute(Configs.SESSION_USER_TYPE, Constants.USER_TYPE_CUSTOMER);
			}
			
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(result.toString());
		} catch (IOException e) {
		}
	}

}
